<script setup lang="ts">
import { userLogin, userRegister } from '@/api/user';
import WechatLog from '@/assets/Wechat.png'
import qqLog from '@/assets/qq.png'
import weiboLog from '@/assets/weiboLog.png'
import type { LoginParams, RegisterParams } from '@/props/user';
import { useUserStore } from '@/stores/user';
import { validatePassword, validatePhoneNumber } from '@/utils/validate';
import { ElMessage } from 'element-plus';
import { computed, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

//data
const userStore = useUserStore()

const router = useRouter()
const isLogin = ref(true)
const userLoginDTO = reactive<LoginParams>({
  email:'',
  password:'',
})

const userRegisterDTO = reactive<RegisterParams>({
  phoneNumber:'',
  password:'',
  verCode:''
})



const login = async() =>{
  console.log(userLoginDTO)

  //手机号/邮箱登录
  const { data }= await userLogin(userLoginDTO)
  if(data.code===1) {
    ElMessage.success("登录成功")
    userStore.setToken(data.data.token)
    userStore.setUserId(data.data.userId)
    router.push("/")
    return
  }
  ElMessage.error(data.msg)
  return

}

//注册
const register = async() => {
  // 校验手机号、密码、验证码是否为空，并且填写的手机号通过正则校验是否满足手机号，密码是否达到6位数字起步并且有一个大小写字母
  if(!validatePhoneNumber(userRegisterDTO.phoneNumber)) {
    if(userRegisterDTO.phoneNumber === ''){
      ElMessage.error("手机号码不能为空");
      return
    }
    ElMessage.error("手机号码格式无效");
    return
  }
  if(!validatePassword(userRegisterDTO.password)){
    ElMessage.error("密码必须至少包含6个字符，并且至少有一个大小写字母");
    return
  }
  if(userRegisterDTO.verCode !== "1234"){
    ElMessage.error("验证码错误")
    return
  }

  const {data} = await userRegister(
    userRegisterDTO.phoneNumber,
    userRegisterDTO.password
  )
  console.log(data)
  if (data.code !== 1){
    ElMessage.error("注册失败")
    return
  }
  ElMessage.success("用户注册成功")
  isLogin.value = true
}


//computed
const title = computed(() => {
  return isLogin.value ? "登录" : "注册";
})

const content = computed(() => {
  if (isLogin.value) {
    return ["没","注册"]
  }
  return ["","登录"]
})



</script>

<template>
<div id="page" class="site">
    <div class="container">
        <div class="login">
            <div class="hero">
                <h1>峰聚 智能<br>FengJu ZhiNeng</h1>
                <p>如果你{{content[0]}}有账号<br>可以<a @click="isLogin = !isLogin">点击这里</a>进行{{ content[1] }}.</p>
            </div>
            <div class="main">
              <h1>{{ title }}</h1>
                <form action="" v-if="isLogin">
                    <p>
                        <input type="text" placeholder="邮箱/手机号" v-model="userLoginDTO.email">
                    </p>
                    <p class="password">
                        <input type="password" placeholder="密码" v-model="userLoginDTO.password">
                        <a href="#">找回密码</a>
                    </p>
                    <p>
                        <input type="submit" class="submit" value="登录" @click="login">
                    </p>
                </form>

                <form action="" v-if="!isLogin">
                    <p>
                        <input type="text" placeholder="手机号" v-model="userRegisterDTO.phoneNumber">
                    </p>
                    <p class="password">
                        <input type="password" placeholder="密码" v-model="userRegisterDTO.password">
                    </p>

                    <p class="password">
                        <input type="text" placeholder="验证码" v-model="userRegisterDTO.verCode">
                        <a class="verification_code" @click="userStore.startCountdown(userRegisterDTO.phoneNumber,'phone')">{{userStore.buttonText}}</a>
                    </p>
                    <p>
                        <input type="submit" class="submit" value="注册" @click="register">
                    </p>
                </form>


                <div class="options">
                    <div class="separator">
                        <p>使用其他方式{{ title }}</p>
                    </div>
                    <ul>
                        <li><a href="#">
                          <img :src=WechatLog width="30">
                        </a></li>
                        <li><a href="#">
                          <img :src=qqLog width="23">
                        </a></li>
                        <li><a href="#">
                          <img :src=weiboLog width="30">
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
</template>


<style scpoed>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@500&display=swap');
/*用中文替换原先英文字体*/
:root {
    --primary-color: #4460f1;
    --white-color: #ffffff;

    --light-text-color: #9398b3;
    --light-bg-color: #f2f4ff;
    --dark-color: #000000;

    --background-color: #242e42;
}

* {
    margin: 0;
    padding: 0;
}

*, ::before::after {
    box-sizing: border-box;
}

body {
    font-family: 'Noto Sans SC', sans-serif;
    font-size: 16px;
    font-weight: 400;
    line-height: 1.8;
    color: var(--dark-color);
    background-color: var(--background-color);
    user-select: none;
    -webkit-user-drag: none;
}

a {
    text-decoration: none;
    color: inherit;
    -webkit-tap-highlight-color: transparent;
}

ul {
    list-style: none;
}

h1 {
    font-size: 3em;
    font-weight: 700;
    line-height: normal;
    color: black;
}

.site {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
}

.container {
    max-width: 1000px;
    width: 100%;
    padding: 0 30px;
    margin: 0 auto;
}

.login {
    display: flex;
    flex-wrap: wrap;
}

.hero {
    flex: 1 0 66.6666%;
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 50px;
    padding: 70px 0;
}

.hero::before {
    content: '';
    position: absolute;
    top: 15%;
    left: 0;
    width: 280px;
    height: 100px;
    background: linear-gradient(to right, var(--primary-color), #c471ed, #f64f59);
    z-index: -1;
    filter: blur(70px);
}

.hero a {
    font-weight: 500;
    color: var(--primary-color);
    transition: color .3s;
}

.hero a:hover,
form .password a:hover {
    color: var(--dark-color);
    text-decoration: underline;
}

.main {
    flex: 1 0 33.3333%;
}
.main h1{
  padding-bottom: 20px;
}

.main form {
    display: flex;
    flex-direction: column;
    gap: 30px;
}

.verification_code{
  position: absolute;
  right: 0;
  top: 15px;
}

.main form p {
    position: relative;
}

form input {
    font: inherit;
    font-size: 14px;
    width: 100%;
    border: 0;
    outline: 0;
    padding: 0 20px;
    line-height: 60px;
    border-radius: 10px;
    /*修复input 加入padding之后大小变化的问题*/
    box-sizing: border-box;
}

input::placeholder {
    color: var(--dark-color);
    font-family: 'Noto Sans SC', sans-serif;
    font-size: inherit;
}

form input:not(.submit) {
    background-color: var(--light-bg-color);
}

form .password i {
    position: absolute;
    top: calc(60px / 2);
    right: 20px;
    margin-top: -8px;
    line-height: 1;
    color: var(--light-text-color);
    cursor: pointer;
}

form .password a {
    font-size: 13px;
    color: var(--light-text-color);
    float: right;
    margin: 5px 20px 0 0;
}

form input.submit {
    font-weight: 700;
    color: var(--white-color);
    background-color: var(--primary-color);
    box-shadow: var(--primary-color) 0 20px 30px -10px;
    cursor: pointer;
    transition: box-shadow .3s;
}

form input.submit:hover {
    box-shadow: var(--primary-color) 0 10px 30px -10px;
}

.separator {
    position: relative;
    margin: 50px 0 30px;
}

.separator::before {
    content: '';
    position: absolute;
    top: 50%;
    width: 100%;
    height: 1px;
    background-color: var(--light-text-color);
    opacity: .3;
    z-index: 1;
}

.separator p {
    font-size: 14px;
    width: fit-content;
    padding: 0 10px;
    color: var(--light-text-color);
    background-color: var(--background-color);
    margin: 0 auto;
    position: relative;
    z-index: 1;
}

.options ul {
    display: flex;
    justify-content: center;
    gap: 20px;
}

.options ul li a {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    border-radius: 10px;
    background-color: var(--white-color);
    box-shadow: var(--light-text-color) 0 10px 20px -10px;
    transition: all .3s ease-out;
}

.options ul li a:hover {
    color: var(--white-color);
    box-shadow: none;
}
/*用STEAM PSN XBOX替换原先APPLE GOOGLE FACEBOOK图标*/
.options ul li:nth-child(1) a:hover {
    background-color: #171a21;
}

.options ul li:nth-child(2) a:hover {
    background-color: #00439c;
}

.options ul li:nth-child(3) a:hover {
    background-color: #107c10;
}
</style>
